package DAO.MySQL;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import DAO.HolidayDAO;
import Model.HolidayBean;

public  class HolidayDAOImpl 
          extends GenericDAO<HolidayBean>
          implements HolidayDAO
          {private final String TARGET_TABLE = "holiday_tb";

	@Override
	protected HolidayBean parseResultSet(ResultSet rs) throws SQLException {
		HolidayBean holidayBean=new HolidayBean();
		holidayBean.setHoliday_id((rs.getInt("holiday_id")));
		holidayBean.setHoliday_date((rs.getDate("holiday_date")));
		holidayBean.setHoliday_name((rs.getString("holiday_name")));
		return holidayBean;
	}

    @Override
	public ArrayList<HolidayBean> findAllHoliday() 
		throws SQLException,ClassNotFoundException 
	{
    	String query="select*from " + TARGET_TABLE;
    	return (executeMultipleQuery(query));
	}

	@Override
	public void addHoliday(HolidayBean inHoliday)
		throws SQLException, ClassNotFoundException
	{
		StringBuffer statement = new StringBuffer();
		statement.append("insert into " + TARGET_TABLE + " value (");
		statement.append( inHoliday.getHoliday_id() + ",");
		statement.append( "'" + inHoliday.getHoliday_date() + "'," );
		statement.append( "'" + inHoliday.getHoliday_name() + "'");
		statement.append(")");
		//System.out.println(statement);
		executeSingleUpdate( statement.toString() );
		
	}


	@Override
	public void updateHoliday(HolidayBean inHoliday) throws SQLException,
			ClassNotFoundException {
		StringBuffer statement = new StringBuffer();
		statement.append("update " + TARGET_TABLE + " set ");
		statement.append("holiday_name='" + inHoliday.getHoliday_name() + "' ,");
		statement.append("holiday_date='" + inHoliday.getHoliday_date() + "' ");
		statement.append("where holiday_id='" + inHoliday.getHoliday_id() + "'");
		executeSingleUpdate( statement.toString() );
		
	}

	@Override
	public HolidayBean findByHolidayID(int input) throws SQLException,
			ClassNotFoundException {
		String query = "select * from " + TARGET_TABLE + " where holiday_id='" + input + "'";
		ArrayList<HolidayBean> tempList = executeMultipleQuery(query);
		if (tempList.size() == 1)
		{ return tempList.get(0); }
		else 
		{ return null; }
	}

	@Override
	public void deleteHoliday(int inHolidayID) throws SQLException,
			ClassNotFoundException {
	StringBuffer sb=new StringBuffer();
	sb.append("delete from " + TARGET_TABLE + " where holiday_id="+inHolidayID);
	executeSingleUpdate(sb.toString());
		
	}

	@Override
	public void deleteHoliday(HolidayBean inHoliday) throws SQLException,
			ClassNotFoundException {
		deleteHoliday(inHoliday.getHoliday_id());
		
	}

	@Override
	public int findNextHoliday() throws SQLException, ClassNotFoundException {
		int output = 1;
		String query = "select max(holiday_id) from " + TARGET_TABLE;
		String temp = executeOneCellQuery(query);
		if (temp == null) {	//If no results found
			output = 1;			//return 1
		} else {					//Otherwise results found
			output = ( Integer.parseInt(temp) + 1 );
		}
		return output;
	}
}
